<?php

namespace App\Jobs;

use LibraryService;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class LibraryExpire implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    protected $openId;

    protected $book;

    /**
     * LibraryExpire constructor.
     *
     * @param $openId
     * @param $book
     */
    public function __construct($openId, $book)
    {
        $this->openId = $openId;

        $this->book = $book;
    }

    /**
     * Execute the job.
     *
     * @return bool
     */
    public function handle()
    {
        if(!$this->openId) return false;

        $libraryExpire = \App\Model\LibraryExpire::firstOrCreate(['open_id' => $this->openId]);

        $libraryExpire->expire = LibraryService::getLatestDate($this->book);

        $libraryExpire->save();
        
    }
}
